<?php 
/**
 * teacher入口
 */
class Teacher_home extends CI_Controller
{

    function __construct()
    {
        parent::__construct();
        //登陆状态检测
        if (!$this->session->userdata('r_user_in'))
            redirect('home');
        //权限检测
        if ($this->session->userdata('r_role_id') != 2)
            redirect('home');
    }

    function index()
    {
        //准备数据
        $data = array();

        $this->load->model('teacher_model');
        $this->load->model('user_model');
        $username = $this->session->userdata('r_username');
        $user = $this->user_model->get_by_username($username);
        $teacher = $this->teacher_model->get_by_user_id($user['id']);
        $teacher = $this->teacher_model->get_teacher_by_id($teacher['id']);

        if ($teacher['pass'] == 0) {
            return $this->teacher_for_check();
        }

        $this->load->model('knowledge_point_model');
        $this->load->model('test_point_model');
        $this->load->model('question_model');
        $this->load->model('module_model');
        $this->load->model('teacher_model');
        $this->load->model('student_model');

        $data['teacher'] = $teacher;
        $data['title'] = $teacher['name'];
        $data['knowledge_point_count'] = $this->knowledge_point_model->count_all();
        $data['test_point_count'] = $this->test_point_model->count_all();
        $data['question_count'] = $this->question_model->count_all();
        // $data['module_count'] = $this->module_model->count_all();
        $data['module_count'] = $this->module_model->getMoudleCountByTeacherID($teacher['id']);
        $data['my_logic_class_count'] = $this->teacher_model->get_logic_class_count($teacher['id']);
        $data['my_students_count'] = $this->teacher_model->get_students_count($teacher['id']);
        $data['my_module_count'] = $this->module_model->getMoudleCountByTeacherID($teacher['id']);
        $this->load->view('teacher_home/index', $data);
    }

    /**
     * 退出，删除session中的数据
     * @return [type]
     */
    function teacher_for_check()
    {
        $this->session->sess_destroy();
        $data['error_message'] = '您的账号尚未通过审核，请在审核通过后使用';
        $this->load->view('error', $data);
    }

    /**
     * 退出，删除session中的数据
     * @return [type]
     */
    function logout()
    {
        $this->session->sess_destroy();
        redirect('home');
    }

    /**
     * 修改密码
     * @return [type] [description]
     */
    function change_password()
    {
        //表单验证
        if ($this->form_validation->run('teacher_home/change_password')) {
            //post方法
            //读取数据
            $old_password = $this->input->post('old_password');
            $new_password = $this->input->post('new_password');

            $username = $this->session->userdata('r_username');
            $this->load->model('user_model');
            $user = $this->user_model->check_user_valid($username, $old_password);
            if ($user) {
                $user['password'] = md5($new_password);
                if ($this->user_model->update($user['id'], $user)) {
                    $this->session->set_flashdata('message', '修改成功');
                    redirect('teacher_home/index');
                } else {
                    $this->session->set_flashdata('message', '操作失败');
                    redirect('teacher_home/index');
                }

            } else {
                $this->session->set_flashdata('message', '密码错误，修改失败');
                redirect('teacher_home/index');
            }

        }

        //get方法
        $data['title'] = '修改密码';
        $this->load->view('teacher_home/change_password', $data);
    }
}
 